package com.dl.mall.service.user.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.dl.mall.mapper.UserMapper;
import com.dl.mall.entity.user.User;
import com.dl.mall.service.user.UserService;
import com.dl.mall.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户操作服务层
 * @author DL
 * @create 2022-03-13 17:11
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean addUser(JSONObject user) {
        String userId = UUIDUtils.getUUID10();
        user.put("userId", userId);
        String password = user.getString("password");
        String md5BySalt = SaSecureUtil.md5BySalt(password, userId);
        user.put("password", md5BySalt);

        return userMapper.insertUser(user) != 0;
    }

    @Override
    public boolean modifyPassword(String userId, String password) {
        String md5BySalt = SaSecureUtil.md5BySalt(password, userId);
        return userMapper.updatePassword(userId, md5BySalt) != 0;
    }

    /**
     * 更新 用户 基本信息
     * @return 受影响的行数
     */
    @Override
    public int modifyUserBasicInfo(User user) {
        return userMapper.updateUserBasicInfo(user);
    }

    /**
     * 更新 用户 全部信息
     * @param user 用户对象
     * @return 受影响的行数
     */
    @Override
    public int modifyUserAllInfo(User user) {
        return userMapper.updateUserAllInfo(user);
    }

    @Override
    public User getUserByUserName(String username) {
        return userMapper.queryUserByUserName(username);
    }

    @Override
    public User getUserByUserId(String userId) {
        return userMapper.queryUserByUserId(userId);
    }

    /**
     * 登录
     * @param username 用户名
     * @return 用户
     */
    @Override
    public User login(String username) {
        return userMapper.login(username);
    }

    /**
     * 根据 用户id 查询用户基本信息
     * @param userId 用户id
     * @return 用户
     */
    @Override
    public User getUserBasicInfo(String userId) {
        return userMapper.queryUserBasicInfo(userId);
    }

}
